package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author lhh
 * @ClassName FrameController
 * @date 2022/9/29 15:12
 */
@Controller
@RequestMapping
public class FrameController {

    @Reference
    private PermissionService permissionService;

    @Reference
    private AdminService adminService;

    @GetMapping
    public String toIndex(Model model){
       /* Long adminId=8L;
        //获取登录用户信息
        Admin admin = adminService.getById(adminId);
        //查询用户角色权限

        //后续替换为当前登录用户id*/
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User)authentication.getPrincipal();
        Admin admin = adminService.getByUserName(user.getUsername());
        List<Permission> permissionList = permissionService.findMenuPermissionByAdminId(admin.getId());
        model.addAttribute("admin",admin);
        model.addAttribute("permissionList",permissionList);
        return "frame/index";
    }

    @GetMapping("main")
    public String main(){
        return "frame/main";
    }

    @RequestMapping("loginPage")
    public String loginPage(){
        return "frame/login";
    }

    @GetMapping("/auth")
    public String auth() {
        return "frame/auth";
    }
    /**
     * 获取当前登录信息
     * @return
     */
    @GetMapping("getInfo")
    @ResponseBody
    public Object getInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        return authentication.getPrincipal();
    }
}
